<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('method_invote_all'))
{
	function method_invote_all() {
		$args = func_get_args();
		$object = $args[0];
		$hook = $args[1];
		unset($args[0], $args[1]);
		$return = array();
		$methods = get_class_methods($object);
		$methods = preg_grep('/^'.$hook.'_[A-Za-z0-9_-]+$/', $methods);
		foreach($methods as $method) {
			$result = call_user_func_array(array($object, $method), $args);
			if (isset($result) && is_array($result)) {
				$return = array_merge_recursive($return, $result);
			} else if (isset($result)) {
				$return[] = $result;
			}
		}
		return $return;
	}
}

if ( ! function_exists('object_invote_all'))
{
	function object_invote_all($object, $hook, &$thing, $parameter = NULL) {
		$methods = get_class_methods($object);
		$methods = preg_grep('/^'.$hook.'_[A-Za-z0-9_-]+$/', $methods);
		foreach($methods as $method) {
			if (is_null($parameter)) {
				$object->$method($thing);
			} else {
				$object->$method($thing, $parameter);
			}
		}
	}
}

if ( ! function_exists('uniqerid'))
{
	function uniqerid() {
		$randomid = mt_rand(0, mt_getrandmax());
		return md5(uniqid($randomid, TRUE));
	}
}

if ( ! function_exists('parse_parameter'))
{
	function parse_parameters($parametersarray) {
		if (is_string($parametersarray) && !empty($parametersarray)) {
			$parametersarray = array($parametersarray);
		}
		
		$source = array();
		if (strcmp($_SERVER['QUERY_STRING'], "") > 0) {
			parse_str($_SERVER['QUERY_STRING'], $source);
		}
		$parameters = array();
		foreach($parametersarray as $name) {
			$parameters[$name] = array_key_exists($name, $source) ? $source[$name] : '';
		}
		return count($parameters) == 1 ? current($parameters) : $parameters;
	}
}

if ( ! function_exists('randomstring'))
{
	function randomstring($length = 12) {
		$sourcestring = 'abcdefghijklmnopqrstuvwxyz0123456789';
		$strlen = strlen($sourcestring);
		$rstring = '';
		for($i = 0; $i < $length; $i++) 
			$rstring .= substr($sourcestring, mt_rand(0, $strlen - 1), 1);
		return $rstring;
	}
}

if ( ! function_exists('getmicrotime'))
{
	function getmicrotime(){ 
		list($usec, $sec) = explode(" ", microtime()); 
		return ((float)$usec + (float)$sec); 
    } 
}

if ( ! function_exists('leftstring'))
{
	function leftstring($sourcestring, $cutlength, $sp = true) {
		$i = 0;
		$n = 0;
		$returnstring = '';
		$stringlength = strlen($sourcestring);
		while (($n < $cutlength) and ($i <= $stringlength)) {
			$tempstring = substr($sourcestring, $i, 1);
			$ascnum = Ord($tempstring);
			if ($ascnum >= 224) {
				$returnstring .= substr($sourcestring, $i, 3);
				$i = $i + 3;
				$n++;
			} elseif ($ascnum >= 192) {
				$returnstring .= substr($sourcestring, $i, 2);
				$i = $i + 2;
				$n++;
			} elseif ($ascnum >= 65 && $ascnum <= 90) {
				$returnstring .= substr($sourcestring, $i, 1);
				$i = $i + 1;
				$n++;
			} else {
				$returnstring .= substr($sourcestring, $i, 1); 
				$i = $i + 1;
				$n = $n + 0.5;
			}
		}
		if (strcmp($sourcestring, $returnstring) != 0) {
			$returnstring .= "..";
		}
		return $returnstring;
	}
}

if ( ! function_exists('format_date'))
{
	function format_date($format = 'Y-m-d H:i:s' ,$timestamp) {
		date_default_timezone_set('PRC');
		$time = date($format, $timestamp);
		return $time;
	}
}

if ( ! function_exists('parse_filename'))
{
	function parse_filename($filename) {
		$item = array();
		$array = explode('.', $filename);
		$item['ext'] = '.'.array_pop($array);
		$item['name'] = implode('.', $array);
		return $item;
	}
}

if ( ! function_exists('pagination'))
{
	function pagination($path, $pindex, $totalcount, $perpage = 10) {
		$pindex_offset = 1;
		$perindex = 10;
		$totalpage = ($totalcount % $perpage == 0) ? intval($totalcount/$perpage) : intval($totalcount/$perpage) + 1;
		if ($totalpage <= 1) return;
		
		$interval = 4;
		$startindex = ($pindex - $interval) > 0 ? $pindex - $interval : 0;
		$endindex = ($pindex + $interval) < $totalpage ? $pindex + $interval : $totalpage;
		
		$pagination = array();
		if ($pindex > 0) {
			$pagination[] = array(
				'title' => '首页',
				'value' => sprintf($path, 0),
			);
			$pagination[] = array(
				'title' => '<',
				'value' => sprintf($path, $pindex - 1),
			);
		}
		for($i = $startindex; $i < $endindex; $i++) {
			if ($i == $pindex) {
				$pagination[] = array(
					'title' => intval($i + 1),
					'value' => sprintf($path, $i),
					'active' => 1
				);
			} else {
				$pagination[] = array(
					'title' => intval($i + 1),
					'value' => sprintf($path, $i),
				);
			}
		}
		if ($pindex < $totalpage - 1) {
			$pagination[] = array(
				'title' => '>',
				'value' => sprintf($path, $pindex + 1)
			);
			$pagination[] = array(
				'title' => '尾页',
				'value' => sprintf($path, $totalpage - 1)
			);
		}
		
		return $pagination;
	}
}

if ( ! function_exists('weektime'))
{
	function weektime($interval = 0) {
		date_default_timezone_set('PRC');
		$weekarray = array();
		$timestamp = mktime(0, 0, 0, date('m', time()), date('d', time()), date('Y', time()));
		$weekth = date('N', $timestamp);
		$weekarray['start'] = strtotime('-'.($weekth + 1).' day', $timestamp);
		$weekarray['end'] = strtotime((7 - $weekth).' day', $timestamp);
		
		if ($interval != 0) {
			foreach($weekarray as $key => $value) {
				$weekarray[$key] = strtotime($interval.' week', $value);
			}
		}
		return $weekarray;
	}
}

if ( ! function_exists('format_type'))
{
	function format_type($detail) {
		$type_name = '' ;
		$e = ($detail->feed_type == 'share')?'资源':'专辑' ;
		switch ($detail->category){
			case 1:
			$type_name = "视频".$e;
			break;
		case 2:
			$type_name = "音乐".$e;
			break;
		case 3:
			$type_name = "图片".$e;
			break;
		case 4:
			$type_name = "文档".$e;
			break;
		case 5:
			$type_name = "种子".$e;
			break;
		case 6:
			$type_name = "其他".$e;
			break;
		case 10:
			$type_name = "专辑资源";
			break;
		default:
			$type_name = "未分类".$e;
		}
		return $type_name ;
	}
}

if ( ! function_exists('format_types'))
{
	function format_types($tid) {
		$type_name = '' ;
		switch ($tid){
		case 0:
			$type_name = "综合资源";
			break;
		case 1:
			$type_name = "视频资源";
			break;
		case 2:
			$type_name = "音乐资源";
			break;
		case 3:
			$type_name = "图片资源";
			break;
		case 4:
			$type_name = "文档资源";
			break;
		case 5:
			$type_name = "种子资源";
			break;
		case 6:
			$type_name = "其他资源";
			break;
		case 7:
			$type_name = "专辑资源";
			break;
		case 10:
			$type_name = "专辑资源";
			break;
		default:
			$type_name = "未分类资源";
		}
		return $type_name ;
	}
}

if ( ! function_exists('format_thumburl'))
{
	function format_thumburl($item) {
		$thumburl = '' ;
		$imageurl1 = "/theme/images/share00.jpg";
		$imageurl2 = "/theme/images/album00.jpg";
		switch ($item->feed_type){
			case 'share':
			//$thumburl = !empty($item->thumburl)? $item->thumburl:$imageurl1 ;
			$thumburl = !empty($item->filepath)? '/'.$item->filepath:$imageurl1 ;
			//$thumburl1 = !empty($item->filepath)? '/'.$item->filepath:$item->thumburl ;
			//$thumburl = !empty($thumburl1)? $thumburl1:$imageurl1 ;
			break;
		case 'album':
			//$thumburl = !empty($item->cover_thumb)? $item->cover_thumb:$imageurl2 ;
			$thumburl = !empty($item->filepath)? '/'.$item->filepath:$imageurl2 ;
			//$thumburl2 = !empty($item->filepath)? '/'.$item->filepath:$item->cover_thumb ;
			//$thumburl = !empty($thumburl2)? $thumburl2:$imageurl2 ;
			break;
		default:
			$thumburl = $imageurl1;
		}
		return $thumburl ;
	}
}

if ( ! function_exists('format_avatarurl'))
{
	function format_avatarurl($item) {
		$defaute_avatar = "/theme/images/000000000.jpg";
		$avatarurl = !empty($item->avatar_url1)? '/'.$item->avatar_url1:$defaute_avatar ;
		return $avatarurl ;
	}
}

if ( ! function_exists('format_downloadurl'))
{
	function format_downloadurl($item) {
		$downloadurl = '' ;
		switch ($item->feed_type){
			case 'share':
				if($item->category == 6){
					$downloadurl="http://yun.baidu.com/share/link?uk=".$item->uk."&shareid=".$item->shareid ;
				}else{
					if(!empty($item->shorturl)){
						$downloadurl="http://yun.baidu.com/s/".$item->shorturl ;
					}else{
						$downloadurl="http://yun.baidu.com/share/link?uk=".$item->uk."&shareid=".$item->shareid ;
					}
				}
			break;
		case 'album':
			$downloadurl = "http://yun.baidu.com/pcloud/album/info?uk=".$item->uk."&album_id=".$item->album_id ;
			break;
		default:
		}
		return $downloadurl ;
	}
}